//
//  StatesViewModel.swift
//  weibo
//
//  Created by JonsonLiu on 2017/3/6.
//  Copyright © 2017年 JonsonLiu. All rights reserved.
//

import UIKit

class StatesViewModel: NSObject {

    var states: HTStates? {

        didSet {

            didMemberImage()
            didview()
        }

    }

    var memberImage: UIImage?

    var retweetText: String?

    var sourceText: String?


    /// 转发文字
    var retweetedStr: String?
    /// 评论文字
    var commentStr: String?
    /// 点赞文字
    var likeStr: String?

    var prictureSize = CGSize()
    
    var picURLS : [HTStatesPicture]? {
        
        return states?.retweeted_status?.pic_urls ?? states?.pic_urls
    }

    func didMemberImage() {

        guard let member = states?.user?.mbrank else {
            memberImage = nil
            return
        }

        if member > 0 && member < 7 {

            memberImage = UIImage(named: "common_icon_membership_level\(member)")
        }

        retweetedStr = countString(count: (states?.reposts_count)!, defaultStr: "转发")
        commentStr = countString(count: (states?.comments_count)!, defaultStr: "评论")
        likeStr = countString(count: (states?.attitudes_count)!, defaultStr: "点赞")

        
    }
    
    func didview(){
        prictureSize = calcPrictureCount(count: picURLS?.count)
    }
    

    func calcPrictureCount(count: Int?) -> CGSize {

        if count == 0 || count == nil{
            return CGSize()
        }

       

        let row = (count! - 1) / 3 + 1

        //计算行高
        var height = statePrictureViewMargin
            height += CGFloat(row) * statePrictureItemWidth
                height += CGFloat(row - 1) * statePrictureinnerMargin
        





        return CGSize(width: statePrictureViewWidth, height: height)
    }

    func countString(count: Int, defaultStr: String) -> String {

        if count == 0 {
            return defaultStr
        }

        if count < 10000 {
            return count.description
        }

        return String(format: "%.02f 万", count / 10000)

    }
    
    class func modelContainerPropertyGenericClass() -> [String: AnyClass] {
        return ["pic_urls": HTStatesPicture.self]
    }
}
